<?php
namespace app\index\controller;

use app\BaseController;
use app\Request;
use app\common\model\admin_user;
use app\common\model\mysql\adminuser;

class Index extends BaseController
{
    public function index(Request $request)
    {
        return view('index');
    }
    public function loge(request $request){
        if(!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }

        $username=$this->request->param("username","","trim");
        $password=$this->request->param("password","","trim");
        $captcha=$this->request->param("captcha","","trim");
        if(empty($username)||empty($password)||empty($captcha)){
        return show(config("status.error"),"参数不能为空");            
        } 

        if($captcha!=$captcha){
        return show(config("status.error"),"验证码不正确");            
        }
       
        $admin_user=new admin_user();

        // $adminuser=new adminuser();

        $adminuser=$admin_user->name($username);
        $adminuser=$adminuser->toArray();
        // var_dump($adminuser['username']);die;

        if(empty($adminuser)){
             return show(config("status.error"),"该用户不存在");  
        }


            $update = [
            "last_log_time"=>time(),
            "last_log_ip"=>$request->ip(),
            "update_time"=>time(),
        ]; 


        $result = $admin_user->upda($adminuser['id'],$update);



        $data['username']=$username;
        $data['password']=md5($password);
        $res=$admin_user->login($data);

        if($res){ 
            session(config("admin.session_admin",$adminuser['username']));
            return show(config("status.error"),"登陆成功");            
        }else{
            return show(config("status.error"),"登陆失败");
        }
    }




    public function show()
    {
        return view('show');
    }



}






